package com.leo.manage_system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.sql.SqlHelper;
import com.leo.manage_system.entity.Admin;
import com.leo.manage_system.mapper.AdminMapper;
import com.leo.manage_system.service.AdminService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 后台管理员信息 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2021-11-01
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {
    @Autowired
    private AdminMapper adminMapper;

    @Override
    public boolean addAdmin(Admin admin) {
        try{
            adminMapper.insert(admin);
        }catch (Exception e) {
            return false;
        }
        return true;
    }

    @Override
    public boolean adminLogin(Admin admin){
        String username = admin.getUsername();
        String password = admin.getPassword();
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        queryWrapper.eq("password", password);
        Admin admin1 = adminMapper.selectOne(queryWrapper);
        if (admin1 == null){
            return false;
        }
        else {
            return true;
        }
    }

    @Override
    public boolean adminPasswordUpdate(String username,String oldPassword,String newPassword){

        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        queryWrapper.eq("password", oldPassword);
        Admin admin = adminMapper.selectOne(queryWrapper);
        if (admin == null){
            return false;
        }
        else{
            admin.setPassword(newPassword);
            adminMapper.updateById(admin);
            return true;
        }
    }

    @Override
    public boolean deleteAdmin(String id){
        try{
            adminMapper.deleteById(id);
        }catch (Exception e) {
            return false;
        }
        return true;
    }
    @Override
    public String getAdminIdByUsername(String username){
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        Admin admin = adminMapper.selectOne(queryWrapper);

        return admin.getId();
    }




}